#include "communication.h"
#include "socket/DynaType.h"
#include "socket/DynaApi.h"
#include "hdr.h"
#include <QMutexLocker>

QMutex Communication::dataNew;
#define PIC_CX      160
#define PIC_CY      118

Communication::Communication(QObject * parent):QThread(parent)
{
    SetCenter(PIC_CX,PIC_CY);
    signalSend = true;
    cmd = GPIC;
}

void Communication::run(){
    AL_DWORD len;
    AL_STATUS ret;
    Picture pic;

    unsigned char **exposure_images;
    int num_exposure=6;
    float* exposure_vals=new int[num_exposure](20,24,28,32,36,40);

    while(1){

        switch(cmd)
        {
        case GPIC:{
            //dataNew.lock();
            QMutexLocker locker(&dataNew);

            /******* 融合图像  *******/
            exposure_ims = (unsigned char**)malloc(num_exposure * sizeof(unsigned char*));
            for(int i=0; i<num_exposure ;i++){
                exposure_ims[i] = (unsigned char*)malloc(imwidth * imheight * sizeof(unsigned char));
                ret = DynaLoadPicture(exposure_images[i], PIC_GRAY8_COMPRESS_SIZE, &len, cx, cy);
                if(ret!=AL_STS_OK){
                    break;
                }
            }
            hdr_init(num_exposure, exposure_vals, exposure_ims, PIC_COMPRESS_WIDTH,PIC_COMPRESS_HEIGTH);
            pic.Gray = hdr_main(HDR_REINHARD_LOCAL);
            display_result(pic.Gray, PIC_COMPRESS_WIDTH,PIC_COMPRESS_HEIGTH);

            hdr_free();
            free(exposure_vals);
            for (exposures_idx = 0; exposures_idx < num_exposure; ++exposures_idx){
                free(exposure_ims[exposures_idx]);
            }
            free(exposure_ims);

            /*******用户注册 ********/
            switch(ret){
            case AL_STS_OK:{
                pic.PicNew = true;
                //pic.normalize();
                if(signalSend){
                    emit gotPic();
                }
                else{
                    emit gotRegisterPic();
                }
            }break;
            case AL_STS_PIC_NON:{
                //dataNew.unlock();
                continue;
            }break;
            default:{
                //dataNew.unlock();
                continue;
            }break;
            }
        }break;
        case SAEC:{

            ret = DynaSetAEC((AL_BYTE)picCount,aecs);

            cmd = GPIC;
        }break;
        default:break;
        }

    }
}


void Communication::releaseData()
{
   // dataNew.unlock();
}

void Communication::signalSendSet(bool ifSend)
{
    signalSend = ifSend;
}

bool Communication::SerialPortInit(QString com)
{
    QByteArray ba = com.toLatin1();
    char *str = ba.data();
    DynaExit();
    if(DynaInit(str) == AL_STS_OK)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void Communication::SerialPortClose()
{
    DynaExit();
}

void Communication::SetCenter( AL_WORD x,AL_WORD y)
{
    cx = x;
    cy = y;
}

void Communication::GetPic( AL_BYTE *data)
{
    AL_DWORD len;
    DynaLoadPicture(data, PIC_RGB565_SIZE, &len, cx, cy);

#if 0
    int j = 1;
    for( int i = 0; i < PIC_RGB565_SIZE; i++)
    {
        //picData[i] =(unsigned char) pic->rgb565.at(i);
        if(i % (640*2) == 0 ){
            printf("\n\n =========Line No. is %d, position: %x========= \n",j,i);
            j++;
        }
        printf(" %x",data[i]);

    }
#endif

}


void Communication::SetAec(int picCount, int *aec)
{
    this->picCount = picCount;
    for(int i = 0; i < picCount; i++)
    {
        aecs[i] = aec[i];
    }
    cmd = SAEC;
}

void Communication::GetPic( Picture *pic)
{
    AL_DWORD len;
    DynaLoadPicture(pic->Gray, PIC_GRAY8_COMPRESS_SIZE, &len, cx, cy);
  //  pic->Transform_RGB2Gray();
  //  pic->compressGray();
}
